package main;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import main.database.DatabaseMan;
import org.bson.Document;

public class GlobalConfig {
    // final readonly
    public static final String collectionNameOfArticle = "article";
    public static final String collectionNameOfRootCatlog = "rootcatlog";
    public static final String collectionNameOfCatlog = "catlog";
    public static final String collectionNameOfUser = "resu";
    public static final String collectionNameOfDailyPicture = "dailypicture";
    public static final String collectionNameOfLostBook = "lostbook";
    public static final String collectionNameOfLostAuthor = "lostauthor";
    public static final String collectionNameOfUserScore = "userscore";
    public static final String collectionNameOfUserCollect = "usercollect";
    public static final String collectionNameOfAuthor = "author";
    public static final String collectionNameOfCommentToArticle = "commenttoarticle";
    public static final String collectionNameOfAudioOfArticle = "audioofarticle";
    public static final String collectionNameOfMovieOfArticle = "movieofarticle";
    public static final String collectionNameOfUserNamePool = "usernamepool";
    public static final String collectionNameOfMessage = "message";
    public static final String collectionNameOfNotify = "notify";
    public static final String collectionNameOfThumbToComment = "thumbcomment";
    public static final String collectionNameOfStatistic = "statistic";
    public static final String collectionNameOfBookComment = "bookcomment";


    public static final String verifyEmailPrefix = "/mailverify?i=";
    public static final String bindEmailUrl = "/mailbind?l=";
    public static final String findPasswordUrl = "/findp?p=";

    public static final int itemPerRankPage = 10;

    // variable read from mongo
    public static String siteUrl = "http://127.0.0.1:8080";
    public static String mailHost = "smtp.163.com";
    public static String mailSender = "18151100998@163.com";
    public static String mailSenderPwd = "kugudaoiamno.1";
    public static String siteName = "天书榜";
    public static boolean displayGongZhongHao = false;

    private static int minimumVote = 0;
    private static int informationtimerange = 1;

    public static void Init() {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection("globalconfig");
        FindIterable<Document> findIterable = collection.find().limit(1);
        MongoCursor<Document> cursor = findIterable.iterator();

        if (!cursor.hasNext()) {
            Logman.error("数据库集合globalconfig中没有数据");
            return;
        }

        Document doc = cursor.next();
        minimumVote = (int) doc.get("bayesian_m");
        siteName = doc.get("sitename").toString();
        siteUrl = doc.get("siteurl").toString();
        mailHost = doc.get("mailhost").toString();
        mailSender = doc.get("mailsender").toString();
        mailSenderPwd = doc.get("mailsenderpassword").toString();
        informationtimerange = (int) doc.get("informationtimerange");
        displayGongZhongHao = (boolean) doc.get("displayGongZhongHao");

        Logman.trace("-------- Config Data Detail --------");
        Logman.trace("site name: " + siteName);
        Logman.trace("site url: " + siteUrl);
        Logman.trace("mail host: " + mailHost);
        Logman.trace("mail sender: " + mailSender);
        Logman.trace("mail sender password: " + mailSenderPwd);
        Logman.trace("information time range(weeks): " + informationtimerange);
        Logman.trace("-------- Config Data Detail End --------");

    }

    public static int getMinimumVote() {
        return minimumVote;
    }

    public static int getInformationTimeRange() {return informationtimerange;}
}
